<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 采用TP5助手函数可实现单字母函数M D U等,也可db::name方式,可双向兼容
 * ============================================================================
 * Author: 当燃
 * Date: 2015-09-09
 */
namespace app\admin\controller;
use app\admin\logic\OrderLogic;
use think\AjaxPage;
use think\Page;
use think\Db;
use think\db\Query;
// use think\PDO;

class Order extends Base {
    public  $order_status;
    public  $pay_status;
    public  $shipping_status;
    public  $hosital_list;
    public  $list;
    public  $admin;
    public  $check_status;
    /*
     * 初始化操作
     */
    public function _initialize() {
        parent::_initialize();
        C('TOKEN_ON',false); // 关闭表单令牌验证
        $this->order_status = C('ORDER_STATUS');
        $this->pay_status = C('PAY_STATUS');
        $this->shipping_status = C('SHIPPING_STATUS');
        $this->check_status = C('CHECK_STATUS');
        // $this->$hosital_list =  M('system_menu')->where(array('id'=>$id))->find();
        // 订单 支付 发货状态
        $this->admin = M('admin')->where('admin_id',session('admin_id'))->find();
        $this->assign('order_status',$this->order_status);
        $this->assign('pay_status',$this->pay_status);
        $this->assign('shipping_status',$this->shipping_status);
        $this->assign('check_status',$this->check_status);
    }

    /*
     *订单首页
     */
    public function index(){
    	$begin = date('Y-m-d',strtotime("-1 year"));//30天前
    	$end = date('Y/m/d',strtotime('+1 days')); 	
    	$this->assign('timegap',$begin.'-'.$end);
        return $this->fetch();
    }

    /*
     *Ajax首页
     */
    public function ajaxindex(){
        // var_dump(session('admin_id'));exit;
        $admin = M('admin')->where('admin_id',session('admin_id'))->find();
        $orderLogic = new OrderLogic();       
        $timegap = I('timegap');
        if($timegap){
        	$gap = explode('-', $timegap);
        	$begin = strtotime($gap[0]);
        	$end = strtotime($gap[1]);
        }else{
            //@new 新后台UI参数
            $begin = strtotime(I('add_time_begin'));
            $end = strtotime(I('add_time_end'));
        }
        
        // 搜索条件
        $condition = array();
        $keyType = I("keytype");
        $keywords = I('keywords','','trim');
        
        $consignee =  ($keyType && $keyType == 'consignee') ? $keywords : I('consignee','','trim');
        

        // $consignee ? $condition['consignee'] = trim($consignee) : false;
        if ($consignee) {
        	$u = M('users')->where('name',$consignee)->find();
        	if ($u) {
        		# code...
        		$condition['user_id'] = $u['user_id'];
        	}
        }
        if($begin && $end){
        	$condition['add_time'] = array('between',"$begin,$end");
        }
        // $condition['o.order_prom_type'] = array('lt',5);

        $order_sn = ($keyType && $keyType == 'order_sn') ? $keywords : I('order_sn') ;
        $order_sn ? $condition['order_sn'] = trim($order_sn) : false;
        // session('admin_id') != 1?$condition['suppliers_id'] = $admin['admin_suppliers'] :false;
        $admin['role_id'] == 6?$condition['hospital_id'] = $admin['admin_hospital']:false;
        I('check_status') != '' ? $condition['check_status'] = I('check_status') : false;
        I('order_status') != '' ? $condition['order_status'] = I('order_status') : false;
        I('pay_status') != '' ? $condition['pay_status'] = I('pay_status') : false;
        I('pay_code') != '' ? $condition['pay_code'] = I('pay_code') : false;
        I('shipping_status') != '' ? $condition['shipping_status'] = I('shipping_status') : false;
        I('user_id') ? $condition['user_id'] = trim(I('user_id')) : false;
        $sort_order = I('order_by','DESC').' '.I('sort');

        $count = M('order')->where($condition)->count();
        $Page  = new AjaxPage($count,20);
        $show = $Page->show();
        if ($condition['user_id']) {
        	unset($condition['user_id']);
        	$condition["o.user_id"] = $u['user_id'];
        	# code...
        }
        // var_dump($condition);exit;
        
        
        //获取订单列表
        $orderList = $orderLogic->getOrderList($condition,$sort_order,$Page->firstRow,$Page->listRows);
        // var_dump($orderList);exit;
        $this->assign('orderList',$orderList);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$Page);
        return $this->fetch();
    }
    //虚拟订单
    public function virtual_list(){
        header("Content-type: text/html; charset=utf-8");
        exit("请联系TPshop官网客服购买高级版支持此功能");
    }
    // 虚拟订单
    public function virtual_info(){
    header("Content-type: text/html; charset=utf-8");
exit("请联系TPshop官网客服购买高级版支持此功能");
    }

    public function virtual_cancel(){
        $order_id = I('order_id/d');
        if(IS_POST){
            $admin_note = I('admin_note');
            $order = M('order')->where(array('order_id'=>$order_id))->find();
            if($order){
                $r = M('order')->where(array('order_id'=>$order_id))->save(array('order_status'=>3,'admin_note'=>$admin_note));
                if($r){
                    $orderLogic = new OrderLogic();
                    $orderLogic->orderActionLog($order_id,$admin_note, '取消订单');
                    $this->ajaxReturn(array('status'=>1,'msg'=>'操作成功'));
                }else{
                    $this->ajaxReturn(array('status'=>-1,'msg'=>'操作失败'));
                }
            }else{
                $this->ajaxReturn(array('status'=>-1,'msg'=>'订单不存在'));
            }
        }
        $order = M('order')->where(array('order_id'=>$order_id))->find();
        $this->assign('order',$order);
        return $this->fetch();
    }

    public function verify_code(){
        if(IS_POST){
            $vr_code = trim(I('vr_code'));
            if (!preg_match('/^[a-zA-Z0-9]{15,18}$/',$vr_code)) {
                $this->ajaxReturn(['status'=>0,'msg' => '兑换码格式错误，请重新输入']);
            }
            $vr_code_info = M('vr_order_code')->where(array('vr_code'=>$vr_code))->find();
            $order = M('order')->where(['order_id'=>$vr_code_info['order_id']])->field('order_status,order_sn,user_note')->find();
            if($order['order_status'] > 1 ){
                $this->ajaxReturn(['status'=>0,'msg' => '兑换码对应订单状态不符合要求']);
            }
            if(empty($vr_code_info)){
                $this->ajaxReturn(['status'=>0,'msg' => '该兑换码不存在']);
            }
            if ($vr_code_info['vr_state'] == '1') {
                $this->ajaxReturn(['status'=>0,'msg' => '该兑换码已被使用']);
            }
            if ($vr_code_info['vr_indate'] < time()) {
                $this->ajaxReturn(['status'=>0,'msg'=>'该兑换码已过期，使用截止日期为： '.date('Y-m-d H:i:s',$vr_code_info['vr_indate'])]);
            }
            if ($vr_code_info['refund_lock'] > 0) {//退款锁定状态:0为正常,1为锁定(待审核),2为同意
                $this->ajaxReturn(['status'=>0,'msg'=> '该兑换码已申请退款，不能使用']);
            }
            $update['vr_state'] = 1;
            $update['vr_usetime'] = time();
            M('vr_order_code')->where(array('vr_code'=>$vr_code))->save($update);
            //检查订单是否完成
            $condition = array();
            $condition['vr_state'] = 0;
            $condition['refund_lock'] = array('in',array(0,1));
            $condition['order_id'] = $vr_code_info['order_id'];
            $condition['vr_indate'] = array('gt',time());
            $vr_order = M('vr_order_code')->where($condition)->select();
            if(empty($vr_order)){
                $data['order_status'] = 2;  //此处不能直接为4，不然前台不能评论
                $data['confirm_time'] = time();
                M('order')->where(['order_id'=>$vr_code_info['order_id']])->save($data);
                M('order_goods')->where(['order_id'=>$vr_code_info['order_id']])->save(['is_send'=>1]);  //把订单状态改为已收货
            }
            $order_goods = M('order_goods')->where(['order_id'=>$vr_code_info['order_id']])->find();
            if($order_goods){
                $result = [
                    'vr_code'=>$vr_code,
                    'order_goods'=>$order_goods,
                    'order'=>$order,
                    'goods_image'=>goods_thum_images($order_goods['goods_id'],240,240),
                ];
                $this->ajaxReturn(['status'=>1,'msg'=>'兑换成功','result'=>$result]);
            }else{
                $this->ajaxReturn(['status'=>0,'msg'=>'虚拟订单商品不存在']);
            }
        }
        return $this->fetch();
    }

    //虚拟订单临时支付方法，以后要删除
    public function generateVirtualCode(){
        $order_id = I('order_id/d');
        // 获取操作表
        $order = M('order')->where(array('order_id'=>$order_id))->find();
        update_pay_status($order['order_sn'], ['admin_id'=>session('admin_id'),'note'=>'订单付款成功']);
        $vr_order_code = Db::name('vr_order_code')->where('order_id',$order_id)->find();
        if(!empty($vr_order_code)){
            $this->success('成功生成兑换码', U('Order/virtual_info',['order_id'=>$order_id]), 1);
        }else{
            $this->error('生成兑换码失败', U('Order/virtual_info',['order_id'=>$order_id]), 1);
        }
    }

    /*
     * ajax 发货订单列表
    */
    public function ajaxdelivery(){
    	$condition = array();
    	I('consignee') ? $condition['consignee'] = trim(I('consignee')) : false;
    	I('order_sn') != '' ? $condition['order_sn'] = trim(I('order_sn')) : false;
    	$shipping_status = I('shipping_status');
    	$condition['shipping_status'] = empty($shipping_status) ? array('neq',1) : $shipping_status;
        $condition['order_status'] = array('in','1,2,4');
    	$count = M('order')->where($condition)->count();
    	$Page  = new AjaxPage($count,10);
    	//搜索条件下 分页赋值
    	foreach($condition as $key=>$val) {
            if(!is_array($val)){
                $Page->parameter[$key]   =   urlencode($val);
            }
    	}
    	$show = $Page->show();
    	$orderList = M('order')->where($condition)->limit($Page->firstRow.','.$Page->listRows)->order('add_time DESC')->select();
    	$this->assign('orderList',$orderList);
    	$this->assign('page',$show);// 赋值分页输出
    	$this->assign('pager',$Page);
    	return $this->fetch();
    }
    
    public function refund_order_list(){
    	$orderLogic = new OrderLogic();
    	$condition = array();
    	I('consignee') ? $condition['consignee'] = trim(I('consignee')) : false;
    	I('order_sn') != '' ? $condition['order_sn'] = trim(I('order_sn')) : false;
    	$condition['shipping_status'] = 0;
    	$condition['order_status'] = 3;
    	$condition['pay_status'] = array('gt',0);
    	$count = M('order')->where($condition)->count();
    	$Page  = new Page($count,10);
    	//搜索条件下 分页赋值
    	foreach($condition as $key=>$val) {
    		if(!is_array($val)){
    			$Page->parameter[$key]   =   urlencode($val);
    		}
    	}
    	$show = $Page->show();
    	$orderList = M('order')->where($condition)->limit($Page->firstRow.','.$Page->listRows)->order('add_time DESC')->select();
    	$this->assign('orderList',$orderList);
    	$this->assign('page',$show);// 赋值分页输出
    	$this->assign('pager',$Page);
    	return $this->fetch();
    }
    
    public function refund_order_info($order_id){
    	$orderLogic = new OrderLogic();
    	$order = $orderLogic->getOrderInfo($order_id);
    	$orderGoods = $orderLogic->getOrderGoods($order_id);
    	$this->assign('order',$order);
    	$this->assign('orderGoods',$orderGoods);
    	return $this->fetch();
    }

    //取消订单退款原路退回
    public function refund_order(){
        $data = I('post.');
        $orderLogic = new OrderLogic();
        $order = $orderLogic->getOrderInfo($data['order_id']);
        if(!order){
            $this->error('订单不存在或参数错误');
        }
        if($data['pay_status'] == 3){
            if($data['refund_type'] == 1){
            	//取消订单退款退余额
                if(updateRefundOrder($order,1)){
                    $this->success('成功退款到账户余额');
                }else{
                    $this->error('退款失败');
                }
            }
            if($data['refund_type']== 0){
                //取消订单支付原路退回
                if($order['pay_code'] == 'weixin' || $order['pay_code'] == 'alipay' || $order['pay_code'] == 'alipayMobile'){
		header("Content-type: text/html; charset=utf-8");
exit("请联系TPshop官网客服购买高级版支持此功能");
                }else{
                    $this->error('该订单支付方式不支持在线退回');
                }
            }
        }else{
            M('order')->where(array('order_id'=>$order['order_id']))->save($data);
            $this->success('拒绝退款操作成功');
        }
    }

    /**
     * 订单详情
     * @param int $id 订单id
     */
    // public function detail($order_id){
    //     $orderLogic = new OrderLogic();
    //     $order = $orderLogic->getOrderInfo($order_id);
    //     $orderGoods = $orderLogic->getOrderGoods($order_id);
    //     $button = $orderLogic->getOrderButton($order);
    //     // 获取操作记录
    //     $action_log = M('order_action')->where(array('order_id'=>$order_id))->order('log_time desc')->select();
    //     $has_user = false;
    //     $adminIds = [];
    //     //查找用户昵称
    //     foreach ($action_log as $k => $v){
    //         if ($v['action_user']) {
    //             $adminIds[$k] = $v['action_user'];
    //         } else {
    //             $has_user = true;
    //         }
    //     }
    //     if($adminIds && count($adminIds) > 0){
    //         $admins = M("admin")->where("admin_id in (".implode(",",$adminIds).")")->getField("admin_id , user_name", true);
    //     }
    //     if($has_user){
    //         $user = M("users")->field('user_id,nickname')->where(['user_id'=>$order['user_id']])->find();
    //     }
    //     // var_dump($orderGoods);exit;
    //     $cs = '';
    //     foreach ($orderGoods as $val) {
    //         if ($val['check_status'] == 1) {
    //             $cs = 1;
    //             break;
    //         }
    //     }
    //     $this->assign('cs',$cs);
    // 	$this->assign('admins',$admins);  
    //     $this->assign('user', $user);
    //     $this->assign('order',$order);
    //     $this->assign('action_log',$action_log);
    //     $this->assign('orderGoods',$orderGoods);
    //     $split = count($orderGoods) >1 ? 1 : 0;
    //     foreach ($orderGoods as $val){
    //     	if($val['goods_num']>1){
    //     		$split = 1;
    //     	}
    //     }
    //     $this->assign('split',$split);
    //     $this->assign('button',$button);
    //     return $this->fetch();
    // }

    public function detail(){
    	$order_id = I('order_id');
    	$orderLogic = new OrderLogic();
    	$order = $orderLogic->getOrderInfo($order_id);
    	$orderGoods = $orderLogic->getOrderGoods($order_id,2);
        if(!$orderGoods)$this->error('此订单商品已完成退货或换货');//已经完成售后的不能再发货
		$delivery_record = M('delivery_doc')->alias('d')->join('__ADMIN__ a','a.admin_id = d.admin_id')->where('d.order_id='.$order_id)->select();
		if($delivery_record){
			$order['invoice_no'] = $delivery_record[count($delivery_record)-1]['invoice_no'];
		}
        // var_dump($orderGoods);exit;
		$this->assign('order',$order);
		$this->assign('orderGoods',$orderGoods);
		$this->assign('delivery_record',$delivery_record);//发货记录
		$shipping_list = M('plugin')->where(array('status'=>1,'type'=>'shipping'))->select();
		$this->assign('shipping_list',$shipping_list);
    	return $this->fetch();
    }
    /**
     * 订单编辑
     * @param int $id 订单id
     */
    public function edit_order(){
    	$order_id = I('order_id');
        $orderLogic = new OrderLogic();
        $order = $orderLogic->getOrderInfo($order_id);
        if($order['shipping_status'] != 0){
            $this->error('已发货订单不允许编辑');
            exit;
        }

        $orderGoods = $orderLogic->getOrderGoods($order_id);

       	if(IS_POST)
        {
            $order['consignee'] = I('consignee');// 收货人
            $order['province'] = I('province'); // 省份
            $order['city'] = I('city'); // 城市
            $order['district'] = I('district'); // 县
            $order['address'] = I('address'); // 收货地址
            $order['mobile'] = I('mobile'); // 手机
            $order['invoice_title'] = I('invoice_title');// 发票
            $order['admin_note'] = I('admin_note'); // 管理员备注
            $order['admin_note'] = I('admin_note'); //
            $order['shipping_code'] = I('shipping');// 物流方式
            $order['shipping_name'] = M('plugin')->where(array('status'=>1,'type'=>'shipping','code'=>I('shipping')))->getField('name');
            $order['pay_code'] = I('payment');// 支付方式
            $order['pay_name'] = M('plugin')->where(array('status'=>1,'type'=>'payment','code'=>I('payment')))->getField('name');
            $goods_id_arr = I("goods_id/a");
            $new_goods = $old_goods_arr = array();
            //################################订单添加商品
            if($goods_id_arr){
            	$new_goods = $orderLogic->get_spec_goods($goods_id_arr);
            	foreach($new_goods as $key => $val)
            	{
            		$val['order_id'] = $order_id;
            		$rec_id = M('order_goods')->add($val);//订单添加商品
            		if(!$rec_id)
            			$this->error('添加失败');
            	}
            }

            //################################订单修改删除商品
            $old_goods = I('old_goods/a');
            foreach ($orderGoods as $val){
            	if(empty($old_goods[$val['rec_id']])){
            		M('order_goods')->where("rec_id=".$val['rec_id'])->delete();//删除商品
            	}else{
            		//修改商品数量
            		if($old_goods[$val['rec_id']] != $val['goods_num']){
            			$val['goods_num'] = $old_goods[$val['rec_id']];
            			M('order_goods')->where("rec_id=".$val['rec_id'])->save(array('goods_num'=>$val['goods_num']));
            		}
            		$old_goods_arr[] = $val;
            	}
            }

            $goodsArr = array_merge($old_goods_arr,$new_goods);
            $result = calculate_price($order['user_id'],$goodsArr,$order['shipping_code'],0,$order['province'],$order['city'],$order['district'],0,0,0);
            if($result['status'] < 0)
            {
            	$this->error($result['msg']);
            }

            //################################修改订单费用
            $order['goods_price']    = $result['result']['goods_price']; // 商品总价
            $order['shipping_price'] = $result['result']['shipping_price'];//物流费
            $order['order_amount']   = $result['result']['order_amount']; // 应付金额
            $order['total_amount']   = $result['result']['total_amount']; // 订单总价
            $o = M('order')->where('order_id='.$order_id)->save($order);

            $l = $orderLogic->orderActionLog($order_id,'修改订单','修改订单');//操作日志
            if($o && $l){
            	$this->success('修改成功',U('Admin/Order/editprice',array('order_id'=>$order_id)));
            }else{
            	$this->success('修改失败',U('Admin/Order/detail',array('order_id'=>$order_id)));
            }
            exit;
        }
        // 获取省份
        $province = M('region')->where(array('parent_id'=>0,'level'=>1))->select();
        //获取订单城市
        $city =  M('region')->where(array('parent_id'=>$order['province'],'level'=>2))->select();
        //获取订单地区
        $area =  M('region')->where(array('parent_id'=>$order['city'],'level'=>3))->select();
        //获取支付方式
        $payment_list = M('plugin')->where(array('status'=>1,'type'=>'payment'))->select();
        //获取配送方式
        $shipping_list = M('plugin')->where(array('status'=>1,'type'=>'shipping'))->select();

        $this->assign('order',$order);
        $this->assign('province',$province);
        $this->assign('city',$city);
        $this->assign('area',$area);
        $this->assign('orderGoods',$orderGoods);
        $this->assign('shipping_list',$shipping_list);
        $this->assign('payment_list',$payment_list);
        return $this->fetch();
    }

    /*
     * 拆分订单
     */
    public function split_order(){
    	$order_id = I('order_id');
    	$orderLogic = new OrderLogic();
    	$order = $orderLogic->getOrderInfo($order_id);
    	if($order['shipping_status'] != 0){
    		$this->error('已发货订单不允许编辑');
    		exit;
    	}
    	$orderGoods = $orderLogic->getOrderGoods($order_id);
    	if(IS_POST){
    		$data = I('post.');
    		//################################先处理原单剩余商品和原订单信息
    		$old_goods = I('old_goods/a');

    		foreach ($orderGoods as $val){
    			if(empty($old_goods[$val['rec_id']])){
    				M('order_goods')->where("rec_id=".$val['rec_id'])->delete();//删除商品
    			}else{
    				//修改商品数量
    				if($old_goods[$val['rec_id']] != $val['goods_num']){
    					$val['goods_num'] = $old_goods[$val['rec_id']];
    					M('order_goods')->where("rec_id=".$val['rec_id'])->save(array('goods_num'=>$val['goods_num']));
    				}
    				$oldArr[] = $val;//剩余商品
    			}
    			$all_goods[$val['rec_id']] = $val;//所有商品信息
    		}
    		$result = calculate_price($order['user_id'],$oldArr,$order['shipping_code'],0,$order['province'],$order['city'],$order['district'],0,0,0);
    		if($result['status'] < 0)
    		{
    			$this->error($result['msg']);
    		}
    		//修改订单费用
    		$res['goods_price']    = $result['result']['goods_price']; // 商品总价
    		$res['order_amount']   = $result['result']['order_amount']; // 应付金额
    		$res['total_amount']   = $result['result']['total_amount']; // 订单总价
    		M('order')->where("order_id=".$order_id)->save($res);
			//################################原单处理结束

    		//################################新单处理
    		for($i=1;$i<20;$i++){
                $temp = $this->request->param($i.'_old_goods/a');
    			if(!empty($temp)){
    				$split_goods[] = $temp;
    			}
    		}

    		foreach ($split_goods as $key=>$vrr){
    			foreach ($vrr as $k=>$v){
    				$all_goods[$k]['goods_num'] = $v;
    				$brr[$key][] = $all_goods[$k];
    			}
    		}

    		foreach($brr as $goods){
    			$result = calculate_price($order['user_id'],$goods,$order['shipping_code'],0,$order['province'],$order['city'],$order['district'],0,0,0);
    			if($result['status'] < 0)
    			{
    				$this->error($result['msg']);
    			}
    			$new_order = $order;
    			$new_order['order_sn'] = date('YmdHis').mt_rand(1000,9999);
    			$new_order['parent_sn'] = $order['order_sn'];
    			//修改订单费用
    			$new_order['goods_price']    = $result['result']['goods_price']; // 商品总价
    			$new_order['order_amount']   = $result['result']['order_amount']; // 应付金额
    			$new_order['total_amount']   = $result['result']['total_amount']; // 订单总价
    			$new_order['add_time'] = time();
    			unset($new_order['order_id']);
    			$new_order_id = DB::name('order')->insertGetId($new_order);//插入订单表
    			foreach ($goods as $vv){
    				$vv['order_id'] = $new_order_id;
    				unset($vv['rec_id']);
    				$nid = M('order_goods')->add($vv);//插入订单商品表
    			}
    		}
    		//################################新单处理结束
    		$this->success('操作成功',U('Admin/Order/detail',array('order_id'=>$order_id)));
            exit;
    	}

    	foreach ($orderGoods as $val){
    		$brr[$val['rec_id']] = array('goods_num'=>$val['goods_num'],'goods_name'=>getSubstr($val['goods_name'], 0, 35).$val['spec_key_name']);
    	}
    	$this->assign('order',$order);
    	$this->assign('goods_num_arr',json_encode($brr));
    	$this->assign('orderGoods',$orderGoods);
    	return $this->fetch();
    }

    /*
     * 价钱修改
     */
    public function editprice($order_id){
        $orderLogic = new OrderLogic();
        $order = $orderLogic->getOrderInfo($order_id);
        $this->editable($order);
        if(IS_POST){
        	$admin_id = session('admin_id');
            if(empty($admin_id)){
                $this->error('非法操作');
                exit;
            }
            $update['discount'] = I('post.discount');
            $update['shipping_price'] = I('post.shipping_price');
			$update['order_amount'] = $order['goods_price'] + $update['shipping_price'] - $update['discount'] - $order['user_money'] - $order['integral_money'] - $order['coupon_price'];
            $row = M('order')->where(array('order_id'=>$order_id))->save($update);
            if(!$row){
                $this->success('没有更新数据',U('Admin/Order/editprice',array('order_id'=>$order_id)));
            }else{
                $this->success('操作成功',U('Admin/Order/detail',array('order_id'=>$order_id)));
            }
            exit;
        }
        $this->assign('order',$order);
        return $this->fetch();
    }

    /**
     * 订单删除
     * @param int $id 订单id
     */
    public function delete_order(){
        $order_id = I('post.order_id/d',0);
    	$orderLogic = new OrderLogic();
    	$del = $orderLogic->delOrder($order_id);
        $this->ajaxReturn($del);
    }

    /**
     * 订单取消付款
     * @param $order_id
     * @return mixed
     */
    public function pay_cancel($order_id){
    	if(I('remark')){
    		$data = I('post.');
    		$note = array('退款到用户余额','已通过其他方式退款','不处理，误操作项');
    		if($data['refundType'] == 0 && $data['amount']>0){
    			accountLog($data['user_id'], $data['amount'], 0,  '退款到用户余额');
    		}
    		$orderLogic = new OrderLogic();
            $orderLogic->orderProcessHandle($data['order_id'],'pay_cancel');
    		$d = $orderLogic->orderActionLog($data['order_id'],'支付取消',$data['remark'].':'.$note[$data['refundType']]);
    		if($d){
    			exit("<script>window.parent.pay_callback(1);</script>");
    		}else{
    			exit("<script>window.parent.pay_callback(0);</script>");
    		}
    	}else{
    		$order = M('order')->where("order_id=$order_id")->find();
    		$this->assign('order',$order);
    		return $this->fetch();
    	}
    }

    /**
     * 订单打印
     * @param int $id 订单id
     */
    public function order_print(){
    	$order_id = I('order_id');
        $orderLogic = new OrderLogic();
        $order = $orderLogic->getOrderInfo($order_id);
        $order['province'] = getRegionName($order['province']);
        $order['city'] = getRegionName($order['city']);
        $order['district'] = getRegionName($order['district']);
        $order['full_address'] = $order['province'].' '.$order['city'].' '.$order['district'].' '. $order['address'];
        $orderGoods = $orderLogic->getOrderGoods($order_id);
        $shop = tpCache('shop_info');
        $this->assign('order',$order);
        $this->assign('shop',$shop);
        $this->assign('orderGoods',$orderGoods);
        $template = I('template','print');
        return $this->fetch($template);
    }

    /**
     * 快递单打印
     */
    public function shipping_print(){
        $order_id = I('get.order_id');
        $orderLogic = new OrderLogic();
        $order = $orderLogic->getOrderInfo($order_id);
        //查询是否存在订单及物流
        $shipping = M('plugin')->where(array('code'=>$order['shipping_code'],'type'=>'shipping'))->find();
        if(!$shipping){
        	$this->error('物流插件不存在');
        }
		if(empty($shipping['config_value'])){
			$this->error('请设置'.$shipping['name'].'打印模板');
		}
        $shop = tpCache('shop_info');//获取网站信息
        $shop['province'] = empty($shop['province']) ? '' : getRegionName($shop['province']);
        $shop['city'] = empty($shop['city']) ? '' : getRegionName($shop['city']);
        $shop['district'] = empty($shop['district']) ? '' : getRegionName($shop['district']);

        $order['province'] = getRegionName($order['province']);
        $order['city'] = getRegionName($order['city']);
        $order['district'] = getRegionName($order['district']);
        if(empty($shipping['config'])){
        	$config = array('width'=>840,'height'=>480,'offset_x'=>0,'offset_y'=>0);
        	$this->assign('config',$config);
        }else{
        	$this->assign('config',unserialize($shipping['config']));
        }
        $template_var = array("发货点-名称", "发货点-联系人", "发货点-电话", "发货点-省份", "发货点-城市",
        		 "发货点-区县", "发货点-手机", "发货点-详细地址", "收件人-姓名", "收件人-手机", "收件人-电话",
        		"收件人-省份", "收件人-城市", "收件人-区县", "收件人-邮编", "收件人-详细地址", "时间-年", "时间-月",
        		"时间-日","时间-当前日期","订单-订单号", "订单-备注","订单-配送费用");
        $content_var = array($shop['store_name'],$shop['contact'],$shop['phone'],$shop['province'],$shop['city'],
        	$shop['district'],$shop['phone'],$shop['address'],$order['consignee'],$order['mobile'],$order['phone'],
        	$order['province'],$order['city'],$order['district'],$order['zipcode'],$order['address'],date('Y'),date('M'),
        	date('d'),date('Y-m-d'),$order['order_sn'],$order['admin_note'],$order['shipping_price'],
        );
        $shipping['config_value'] = str_replace($template_var,$content_var, $shipping['config_value']);
        $this->assign('shipping',$shipping);
        return $this->fetch("Plugin/print_express");
    }

    /**
     * 生成发货单
     */
    public function deliveryHandle(){
    	var_dump(I('post.'));exit;
        $orderLogic = new OrderLogic();
		$data = I('post.');
		$res = $orderLogic->deliveryHandle($data);
		if($res){
			$this->success('操作成功',U('Admin/Order/delivery_info',array('order_id'=>$data['order_id'])));
		}else{
			$this->success('操作失败',U('Admin/Order/delivery_info',array('order_id'=>$data['order_id'])));
		}
    }


    public function delivery_info(){
    	$order_id = I('order_id');
    	$orderLogic = new OrderLogic();
    	$order = $orderLogic->getOrderInfo($order_id);
    	$orderGoods = $orderLogic->getOrderGoods($order_id,2);
        if(!$orderGoods)$this->error('此订单商品已完成退货或换货');//已经完成售后的不能再发货
		$delivery_record = M('delivery_doc')->alias('d')->join('__ADMIN__ a','a.admin_id = d.admin_id')->where('d.order_id='.$order_id)->select();
		if($delivery_record){
			$order['invoice_no'] = $delivery_record[count($delivery_record)-1]['invoice_no'];
		}
        // var_dump($orderGoods);exit;
		$this->assign('order',$order);
		$this->assign('orderGoods',$orderGoods);
		$this->assign('delivery_record',$delivery_record);//发货记录
		$shipping_list = M('plugin')->where(array('status'=>1,'type'=>'shipping'))->select();
		$this->assign('shipping_list',$shipping_list);
    	return $this->fetch();
    }
    /**
     * 发货单列表
     */
    public function delivery_list(){
        return $this->fetch();
    }


    /**
     * 删除某个退换货申请
     */
    public function return_del(){
        $id = I('get.id');
        M('return_goods')->where("id = $id")->delete();
        $this->success('成功删除!');
    }

    /**
     * 退换货操作
     */
    public function return_info()
    {
        $orderLogic = new OrderLogic();
        $return_id = I('id');
        $return_goods = M('return_goods')->where("id= $return_id")->find();
        if(!$return_goods)
        {
            $this->error('非法操作!');
            exit;
        }
        $user = M('users')->where("user_id = {$return_goods[user_id]}")->find();
        $goods = M('goods')->where("goods_id = {$return_goods[goods_id]}")->find();
        $type_msg = array('仅退款','退货退款','换货');
        $status_msg = C('REFUND_STATUS');
        if(IS_POST)
        {
            $data = I('post.');
            if($return_goods['type'] == 2 && $return_goods['is_receive'] == 1){
            	$data['seller_delivery']['express_time'] = date('Y-m-d H:i:s');
            	$data['seller_delivery'] = serialize($data['seller_delivery']); //换货的物流信息
            }
            $note ="退换货:{$type_msg[$return_goods['type']]}, 状态:{$status_msg[$data['status']]},处理备注：{$data['remark']}";
            $result = M('return_goods')->where("id= $return_id")->save($data);
            if($result && $data['status']==1)
            {
                //审核通过才更改订单商品状态，进行退货，退款时要改对应商品修改库存
                $order = \app\common\model\Order::get($return_goods['order_id']);
                $commonOrderLogic = new \app\common\logic\OrderLogic();
                $commonOrderLogic->alterReturnGoodsInventory($order,$return_goods['rec_id']); //审核通过，恢复原来库存
                if($return_goods['type'] < 2){
                    $orderLogic->disposereRurnOrderCoupon($return_goods); // 是退货可能要处理优惠券
                }
            }
            $orderLogic->orderActionLog($return_goods['order_id'],'退换货',$note);
            $this->success('修改成功!');
            exit;
        }
        $return_goods['seller_delivery'] = unserialize($return_goods['seller_delivery']);  //订单的物流信息，服务类型为换货会显示
        if($return_goods['imgs']) $return_goods['imgs'] = explode(',', $return_goods['imgs']);
        $this->assign('id',$return_id); // 用户
        $this->assign('user',$user); // 用户
        $this->assign('goods',$goods);// 商品
        $this->assign('return_goods',$return_goods);// 退换货
        $order = M('order')->where(array('order_id'=>$return_goods['order_id']))->find();
        $this->assign('order',$order);//退货订单信息
        return $this->fetch();
    }

    //售后退款原路退回
    public function refund_back(){
    	$return_id = I('id');
        $return_goods = M('return_goods')->where("id= $return_id")->find();
    	$rec_goods = M('order_goods')->where(array('order_id'=>$return_goods['order_id'],'goods_id'=>$return_goods['goods_id']))->find();
    	$order = M('order')->where(array('order_id'=>$rec_goods['order_id']))->find();
    	if($order['pay_code'] == 'weixin' || $order['pay_code'] == 'alipay' || $order['pay_code'] == 'alipayMobile'){
    		$orderLogic = new OrderLogic();
    		$return_goods['refund_money'] = $orderLogic->getRefundGoodsMoney($return_goods);
    		if($order['pay_code'] == 'weixin'){
    			include_once  PLUGIN_PATH."payment/weixin/weixin.class.php";
    			$payment_obj =  new \weixin();
    			$data = array('transaction_id'=>$order['transaction_id'],'total_fee'=>$order['order_amount'],'refund_fee'=>$return_goods['refund_money']);
    			$result = $payment_obj->payment_refund($data);
    			if($result['return_code'] == 'SUCCESS'  && $result['result_code' == 'SUCCESS']){
    				updateRefundGoods($return_goods['rec_id']);//订单商品售后退款
    				$this->success('退款成功');
    			}else{
    				$this->error($result['return_msg']);
    			}
    		}else{
    			include_once  PLUGIN_PATH."payment/alipay/alipay.class.php";
    			$payment_obj = new \alipay();
    			$detail_data = $order['transaction_id'].'^'.$return_goods['refund_money'].'^'.'用户申请订单退款';
    			$data = array('batch_no'=>date('YmdHi').$rec_goods['rec_id'],'batch_num'=>1,'detail_data'=>$detail_data);
    			$payment_obj->payment_refund($data);
    		}
    	}else{
    		$this->error('该订单支付方式不支持在线退回');
    	}
    }

    /**
     * 退货，余额+积分支付
     * 有用三方金额支付的不走这个方法
     */
    public function refund_balance(){
		$data = I('POST.'); 
		$return_goods = M('return_goods')->where(array('rec_id'=>$data['rec_id']))->find();
		if(empty($return_goods)) $this->ajaxReturn(['status'=>0,'msg'=>"参数有误"]); 
		M('return_goods')->where(array('rec_id'=>$data['rec_id']))->save($data);
		updateRefundGoods($data['rec_id'],1);//售后商品退款
		$this->ajaxReturn(['status'=>1,'msg'=>"操作成功",'url'=>U("Admin/Order/return_list")]);
		
    }

    /**
     * 管理员生成申请退货单
     */
    public function add_return_goods()
   {
            $order_id = I('order_id');
            $goods_id = I('goods_id');

            $return_goods = M('return_goods')->where("order_id = $order_id and goods_id = $goods_id")->find();
            if(!empty($return_goods))
            {
                $this->error('已经提交过退货申请!',U('Admin/Order/return_list'));
                exit;
            }
            $order = M('order')->where("order_id = $order_id")->find();

            $data['order_id'] = $order_id;
            $data['order_sn'] = $order['order_sn'];
            $data['goods_id'] = $goods_id;
            $data['addtime'] = time();
            $data['user_id'] = $order[user_id];
            $data['remark'] = '管理员申请退换货'; // 问题描述
            M('return_goods')->add($data);
            $this->success('申请成功,现在去处理退货',U('Admin/Order/return_list'));
            exit;
    }

    /**
     * 订单操作
     * @param $id
     */
    public function order_action(){    	
        $orderLogic = new OrderLogic();
        $action = I('get.type');
        $order_id = I('get.order_id');
        if($action && $order_id){
            if($action !=='pay'){
                $convert_action= C('CONVERT_ACTION')["$action"];
                $res = $orderLogic->orderActionLog($order_id,$convert_action,I('note'));
            }
        	 $a = $orderLogic->orderProcessHandle($order_id,$action,array('note'=>I('note'),'admin_id'=>0));
        	 if($res !== false && $a !== false){
                 if ($action == 'remove') {
                     exit(json_encode(array('status' => 1, 'msg' => '操作成功', 'data' => array('url' => U('admin/order/index')))));
                 }
        	 	exit(json_encode(array('status' => 1,'msg' => '操作成功')));
        	 }else{
                 if ($action == 'remove') {
                     exit(json_encode(array('status' => 0, 'msg' => '操作失败', 'data' => array('url' => U('admin/order/index')))));
                 }
        	 	exit(json_encode(array('status' => 0,'msg' => '操作失败')));
        	 }
        }else{
        	$this->error('参数错误',U('Admin/Order/detail',array('order_id'=>$order_id)));
        }
    }
    
    public function order_log(){
    	$timegap = urldecode(I('timegap'));
    	if($timegap){
    		$gap = explode('-', $timegap);
            $timegap_begin = $gap[0];
            $timegap_end = $gap[1];
    		$begin = strtotime($timegap_begin);
    		$end = strtotime($timegap_end);
    	}else{
    	    //@new 兼容新模板
            $timegap_begin = urldecode(I('timegap_begin'));
            $timegap_end = urldecode(I('timegap_end'));
    	    $begin = strtotime($timegap_begin);
    	    $end = strtotime($timegap_end);
    	}
    	$condition = array();
    	$log =  M('order_action');
    	if($begin && $end){
    		$condition['log_time'] = array('between',"$begin,$end");
    	}
    	$admin_id = I('admin_id');
		if($admin_id >0 ){
			$condition['action_user'] = $admin_id;
		}
    	$count = $log->where($condition)->count();
    	$Page = new Page($count,20);

    	foreach($condition as $key=>$val) {
    		$Page->parameter[$key] = urlencode($begin.'_'.$end);
    	}
    	$show = $Page->show();
    	$list = $log->where($condition)->order('action_id desc')->limit($Page->firstRow.','.$Page->listRows)->select();
        $orderIds = [];
        foreach ($list as $log) {
            if (!$log['action_user']) {
                $orderIds[] = $log['order_id'];
            }
        }
        if ($orderIds) {
            $users = M("users")->alias('u')->join('__ORDER__ o', 'o.user_id = u.user_id')->getField('o.order_id,u.nickname');
        }
        $this->assign('timegap_begin',$timegap_begin);
        $this->assign('timegap_end',$timegap_end);
        $this->assign('users',$users);
    	$this->assign('list',$list);
    	$this->assign('pager',$Page);
    	$this->assign('page',$show);   	
    	$admin = M('admin')->getField('admin_id,user_name');
    	$this->assign('admin',$admin);    	
    	return $this->fetch();
    }

    /**
     * 检测订单是否可以编辑
     * @param $order
     */
    private function editable($order){
        if($order['shipping_status'] != 0){
            $this->error('已发货订单不允许编辑');
            exit;
        }
        return;
    }

    public function export_order()
    {
    	//搜索条件
        $consignee = I('consignee');
        $order_sn =  I('order_sn');
        $timegap = I('timegap');
        $order_status = I('order_status');
        $order_ids = I('order_ids');
        $where = array();//搜索条件
        if($consignee){
            $where['consignee'] = ['like','%'.$consignee.'%'];
        }
        if($order_sn){
            $where['order_sn'] = $order_sn;
        }
        if($order_status){
            $where['order_status'] = $order_status;
        }
        if($timegap){
            $gap = explode('-', $timegap);
            $begin = strtotime($gap[0]);
            $end = strtotime($gap[1]);
            $where['add_time'] = ['between',[$begin, $end]];
        }
        if($order_ids){
            $where['order_id'] = ['in', $order_ids];
        }
        $orderList = Db::name('order')->field("*,FROM_UNIXTIME(add_time,'%Y-%m-%d') as create_time")->where($where)->order('order_id')->select();
    	$strTable ='<table width="500" border="1">';
    	$strTable .= '<tr>';
    	$strTable .= '<td style="text-align:center;font-size:12px;width:120px;">订单编号</td>';
    	$strTable .= '<td style="text-align:center;font-size:12px;" width="100">日期</td>';
    	$strTable .= '<td style="text-align:center;font-size:12px;" width="*">收货人</td>';
    	$strTable .= '<td style="text-align:center;font-size:12px;" width="*">收货地址</td>';
    	$strTable .= '<td style="text-align:center;font-size:12px;" width="*">电话</td>';
    	$strTable .= '<td style="text-align:center;font-size:12px;" width="*">订单金额</td>';
    	$strTable .= '<td style="text-align:center;font-size:12px;" width="*">实际支付</td>';
    	$strTable .= '<td style="text-align:center;font-size:12px;" width="*">支付方式</td>';
    	$strTable .= '<td style="text-align:center;font-size:12px;" width="*">支付状态</td>';
    	$strTable .= '<td style="text-align:center;font-size:12px;" width="*">发货状态</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">商品数量</td>';
    	$strTable .= '<td style="text-align:center;font-size:12px;" width="*">商品信息</td>';
    	$strTable .= '</tr>';
	    if(is_array($orderList)){
	    	$region	= get_region_list();
	    	foreach($orderList as $k=>$val){
	    		$strTable .= '<tr>';
	    		$strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val['order_sn'].'</td>';
	    		$strTable .= '<td style="text-align:left;font-size:12px;">'.$val['create_time'].' </td>';	    		
	    		$strTable .= '<td style="text-align:left;font-size:12px;">'.$val['consignee'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'."{$region[$val['province']]},{$region[$val['city']]},{$region[$val['district']]},{$region[$val['twon']]}{$val['address']}".' </td>';
	    		$strTable .= '<td style="text-align:left;font-size:12px;">'.$val['mobile'].'</td>';
	    		$strTable .= '<td style="text-align:left;font-size:12px;">'.$val['goods_price'].'</td>';
	    		$strTable .= '<td style="text-align:left;font-size:12px;">'.$val['order_amount'].'</td>';
	    		$strTable .= '<td style="text-align:left;font-size:12px;">'.$val['pay_name'].'</td>';
	    		$strTable .= '<td style="text-align:left;font-size:12px;">'.$this->pay_status[$val['pay_status']].'</td>';
	    		$strTable .= '<td style="text-align:left;font-size:12px;">'.$this->shipping_status[$val['shipping_status']].'</td>';
	    		$orderGoods = D('order_goods')->where('order_id='.$val['order_id'])->select();
	    		$strGoods="";
                $goods_num = 0;
	    		foreach($orderGoods as $goods){
                    $goods_num = $goods_num + $goods['goods_num'];
	    			$strGoods .= "商品编号：".$goods['goods_sn']." 商品名称：".$goods['goods_name'];
	    			if ($goods['spec_key_name'] != '') $strGoods .= " 规格：".$goods['spec_key_name'];
	    			$strGoods .= "<br />";
	    		}
	    		unset($orderGoods);
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$goods_num.' </td>';
	    		$strTable .= '<td style="text-align:left;font-size:12px;">'.$strGoods.' </td>';
	    		$strTable .= '</tr>';
	    	}
	    }
    	$strTable .='</table>';
    	unset($orderList);
    	downloadExcel($strTable,'order');
    	exit();
    }
    
    /**
     * 退货单列表
     */
    public function return_list(){
        return $this->fetch();
    }

    /*
     * ajax 退货订单列表
     */
    public function ajax_return_list(){
        // 搜索条件
        $order_sn =  trim(I('order_sn'));
        $order_by = I('order_by') ? I('order_by') : 'addtime';
        $sort_order = I('sort_order') ? I('sort_order') : 'desc';
        $status =  I('status');

        $where = " 1 = 1 ";
        $order_sn && $where.= " and order_sn like '%$order_sn%' ";
        empty($order_sn)&& !empty($status) && $where.= " and status = '$status' ";
        $count = M('return_goods')->where($where)->count();
        $Page  = new AjaxPage($count,13);
        $show = $Page->show();
        $list = M('return_goods')->where($where)->order("$order_by $sort_order")->limit("{$Page->firstRow},{$Page->listRows}")->select();
        $goods_id_arr = get_arr_column($list, 'goods_id');
        if(!empty($goods_id_arr)){
            $goods_list = M('goods')->where("goods_id in (".implode(',', $goods_id_arr).")")->getField('goods_id,goods_name');
        }
        $state = C('REFUND_STATUS');
        $this->assign('state',$state);
        $this->assign('goods_list',$goods_list);
        $this->assign('list',$list);
        $this->assign('pager',$Page);
        $this->assign('page',$show);// 赋值分页输出
        return $this->fetch();
    }
    
    /**
     * 添加一笔订单
     */
    public function add_order()
    {
        $order = array();
        //  获取省份
        $province = M('region')->where(array('parent_id'=>0,'level'=>1))->select();
        //  获取订单城市
        $city =  M('region')->where(array('parent_id'=>$order['province'],'level'=>2))->select();
        //  获取订单地区
        $area =  M('region')->where(array('parent_id'=>$order['city'],'level'=>3))->select();
        //  获取配送方式
        $shipping_list = M('plugin')->where(array('status'=>1,'type'=>'shipping'))->select();
        //  获取支付方式
        $payment_list = M('plugin')->where(array('status'=>1,'type'=>'payment'))->select();
        if(IS_POST)
        {
            $order['user_id'] = I('user_id');// 用户id 可以为空
            $order['consignee'] = I('consignee');// 收货人
            $order['province'] = I('province'); // 省份
            $order['city'] = I('city'); // 城市
            $order['district'] = I('district'); // 县
            $order['address'] = I('address'); // 收货地址
            $order['mobile'] = I('mobile'); // 手机           
            $order['invoice_title'] = I('invoice_title');// 发票
            $order['admin_note'] = I('admin_note'); // 管理员备注            
            $order['order_sn'] = date('YmdHis').mt_rand(1000,9999); // 订单编号;
            $order['admin_note'] = I('admin_note'); // 
            $order['add_time'] = time(); //                    
            $order['shipping_code'] = I('shipping');// 物流方式
            $order['shipping_name'] = M('plugin')->where(array('status'=>1,'type'=>'shipping','code'=>I('shipping')))->getField('name');            
            $order['pay_code'] = I('payment');// 支付方式            
            $order['pay_name'] = M('plugin')->where(array('status'=>1,'type'=>'payment','code'=>I('payment')))->getField('name');            
                            
            $goods_id_arr = I("goods_id/a");
            $orderLogic = new OrderLogic();
            $order_goods = $orderLogic->get_spec_goods($goods_id_arr);          
            $result = calculate_price($order['user_id'],$order_goods,$order['shipping_code'],0,$order[province],$order[city],$order[district],0,0,0);      
            if($result['status'] < 0)	
            {
                 $this->error($result['msg']);      
            } 
           
           $order['goods_price']    = $result['result']['goods_price']; // 商品总价
           $order['shipping_price'] = $result['result']['shipping_price']; //物流费
           $order['order_amount']   = $result['result']['order_amount']; // 应付金额
           $order['total_amount']   = $result['result']['total_amount']; // 订单总价
           
            // 添加订单
            $order_id = M('order')->add($order);
            $order_insert_id = DB::getLastInsID();
            if($order_id)
            {
                foreach($order_goods as $key => $val)
                {
                    $val['order_id'] = $order_id;
                    $rec_id = M('order_goods')->add($val);
                    if(!$rec_id)
                        $this->error('添加失败');                                  
                }
  
                M('order_action')->add([
                    'order_id'      => $order_id,
                    'action_user'   => session('admin_id'),
                    'order_status'  => 0,  //待支付
                    'shipping_status' => 0, //待确认
                    'action_note'   => $order['admin_note'],
                    'status_desc'   => '提交订单',
                    'log_time'      => time()
                ]);
                $this->success('添加商品成功',U("Admin/Order/detail",array('order_id'=>$order_insert_id)));
                exit();
            }
            else{
                $this->error('添加失败');
            }                
        }     
        $this->assign('shipping_list',$shipping_list);
        $this->assign('payment_list',$payment_list);
        $this->assign('province',$province);
        $this->assign('city',$city);
        $this->assign('area',$area);        
        return $this->fetch();
    }
    
    /**
     * 选择搜索商品
     */
    public function search_goods()
    {
    	$brandList =  M("brand")->select();
    	$categoryList =  M("goods_category")->select();
    	$this->assign('categoryList',$categoryList);
    	$this->assign('brandList',$brandList);
    	$where = ' is_on_sale = 1 and is_virtual =' . I('is_virtual/d',0);//搜索条件
    	I('intro')  && $where = "$where and ".I('intro')." = 1";
    	if(I('cat_id')){
    		$this->assign('cat_id',I('cat_id'));    		
            $grandson_ids = getCatGrandson(I('cat_id')); 
            $where = " $where  and cat_id in(".  implode(',', $grandson_ids).") "; // 初始化搜索条件
                
    	}
        if(I('brand_id')){
            $this->assign('brand_id',I('brand_id'));
            $where = "$where and brand_id = ".I('brand_id');
        }
    	if(!empty($_REQUEST['keywords']))
    	{
    		$this->assign('keywords',I('keywords'));
    		$where = "$where and (goods_name like '%".I('keywords')."%' or keywords like '%".I('keywords')."%')" ;
    	}
        $goods_count =M('goods')->where($where)->count();
        $Page = new Page($goods_count,C('PAGESIZE'));
    	$goodsList = M('goods')->where($where)->order('goods_id DESC')->limit($Page->firstRow,$Page->listRows)->select();
                
        foreach($goodsList as $key => $val)
        {
            $spec_goods = M('spec_goods_price')->where("goods_id = {$val['goods_id']}")->select();
            $goodsList[$key]['spec_goods'] = $spec_goods;            
        }
        if($goodsList){
            //计算商品数量
            foreach ($goodsList as $value){
                if($value['spec_goods']){
                    $count += count($value['spec_goods']);
                }else{
                    $count++;
                }
            }
            $this->assign('totalSize',$count);
        }

    	$this->assign('page',$Page->show());
    	$this->assign('goodsList',$goodsList);
    	return $this->fetch();
    }
    
    public function ajaxOrderNotice(){
        $order_amount = M('order')->where("order_status=0 and (pay_status=1 or pay_code='cod')")->count();
        echo $order_amount;
    }

    //分院列表
    // public function index_director_check(){
    //     $list = M('hospital')->select();
    //     $status = 0;
    //     $this->assign('status',$status);
    //     $this->assign('list',$list);
    //     return $this->fetch();
    // }

    public function index_director_check_1(){
        return $this->fetch();
    }

    /*
     *门诊审批列表
     */
    public function director_ajax(){
        $admin = M('admin')->where('admin_id',session('admin_id'))->find();
        $p = I('p');
        $msg = I('msg');
        if ($admin['role_id'] == 6) {
            $where['hospital_id'] = $admin['admin_hospital'];
            if ($msg == 1) {
                $where['check_refuse'] = array('in','1,2');
                $check_status = array('in','0,2');
                $check_refuse = 1;
            }elseif ($msg == 2) {
                $where['check_status'] = array('in','1,2');
                $where['check_refuse'] = array('in','0,2');
                $check_refuse = 0;
                $check_status = array('in','1,2');
            }else{
                $where['check_status'] = array('in','0,2');
                $where['check_refuse'] = array('in','0,2');
                $check_status = 0;
                $check_refuse = 0;
            }

            $count = M('order')->where($where)->count();
            $Page  = new AjaxPage($count,20);
            $show = $Page->show();
            $order_id = M('order')->where($where)->field('order_id')->select();
            if ($order_id) {
                foreach ($order_id as $value) {
                    // var_dump($value);exit;
                    $list[] = M('order_goods o')
                                ->join('goods g','o.goods_id=g.goods_id','LEFT')
                                ->join('order r','o.order_id=r.order_id','LEFT')
                                ->join('users u','u.user_id=r.user_id','LEFT')
                                ->where(array('o.order_id'=>$value['order_id'],'o.check_status'=>$check_status,'o.check_refuse'=>$check_refuse,'order_status'=>array('lt',3)))
                                ->field('o.*,g.goods_gg as ggg,g.goods_xh as gxh,g.brand as gbrand,u.name as uname')
                                ->select();
                }
                // var_dump($list);exit;          
                foreach ($list as $value) {
                    foreach ($value as $v) {
                        $l[] = $v; 
                    }
                }
          		$count = count($l);
        		$page = new AjaxPage($count,20);
        		// $url = $page->show();
        		// var_dump($url);exit;
        		$pa = ($p-1)*20;
        		$li = array_slice($l,$pa,20);
          		// var_dump($url);exit;

        		$show = $page->show();

            }

        
        }
        // $url = U(ACTION_NAME);
        // var_dump($url);exit;
        $this->assign('show',$show);
        $this->assign('list',$li);
        // $this->assign('list',$l);
        return $this->fetch();

    }

    //审批
    public function check_1(){
    	$hospital_id = $this->admin['admin_hospital'];
        $rec_id = I('rec_id');
        // var_dump($rec_id);exit;
        $order_id = I('order_id');
        $status = I('post.status');
        $refuse_info = I('refuse_info');
        $user_id = M('order')->where('order_id',$order_id)->field('user_id')->find();
        if ($status == 1) {
            $data['check_status'] = 1;
            $res = M('order_goods')->where('rec_id',$rec_id)->save($data);
            $goods = M("order_goods")->where('rec_id',$rec_id)->find();
            $goods_info = M('goods g')
            			->join('suppliers s',"g.suppliers_id=s.suppliers_id","LEFT")
            			->join("brand b","g.brand_id=b.id","LEFT")
                        ->where('g.goods_id',$goods['goods_id'])
                        ->field("g.*,s.suppliers_name as suppliers_name,b.name as bnane")
                        ->find();
            $buy['goods_id'] = $goods_info['goods_id'];
            $buy['goods_sn'] = $goods_info['goods_sn'];
            $buy['goods_name'] = $goods_info['goods_name'];
            $buy['goods_sn'] = $goods_info['goods_sn'];
            $buy['brand_name'] = $goods_info['bnane'];
            $buy['suppliers_name'] = $goods_info['suppliers_name'];
            $buy['goods_num'] = $goods['goods_num'];
            $buy['hospital_id'] = $hospital_id;
            $buy['goods_price'] = $goods_info['shop_price'];
            $buy['goods_gg'] = $goods_info['goods_gg'];
            $buy['goods_xh'] = $goods_info['goods_xh'];
            $buy['goods_cd'] = $goods_info['goods_cd'];
            $buy['goods_dw'] = $goods_info['goods_dw'];
            // var_dump($buy);exit;
            $r_buy = M('buy_list')->where(array("hospital_id"=>$hospital_id,"status"=>0,"goods_id"=>$goods_info['goods_id']))->find();
            // var_dump($r_buy);
            if ($r_buy) {
            	$d['goods_num'] = $r_buy['goods_num']+$buy['goods_num'];
            	$buy_id = $r_buy['id'];
            	M('buy_list')->where('id',$buy_id)->save($d);
            }else{
            	$buy['make_at']=time();
            	M('buy_list')->add($buy);
            }
            

            if ($res) {
                // $list = M('order_goods r')
                //         ->join('goods g','g.goods_id=r.goods_id','LEFT')
                //         ->join('order o','o.order_id=r.order_id','LEFT')
                //         ->join('suppliers s','s.suppliers_id=g.suppliers_id','LEFT')
                //         ->where('r.rec_id',$rec_id)
                //         ->field('r.*,g.goods_gg as ggg,g.goods_xh as gxh,g.brand as gbrand,g.suppliers_id as gsid,g.goods_cd as gcd,s.suppliers_name as suname,o.hospital_id as ohid')
                //         ->find();
                // // var_dump($list);exit;
                // $buy['goods_id'] = $list['goods_id'];
                // $buy['goods_sn'] = $list['goods_sn'];
                // $buy['goods_name'] = $list['goods_name'];
                // $buy['suppliers_name'] = $list['suname'];
                // $buy['brand_name'] = $list['gbrand'];                
                // $buy['goods_num'] = $list['goods_num'];
                // $buy['goods_price'] = $list['goods_price'];
                // $buy['goods_gg'] = $list['ggg'];
                // $buy['goods_xh'] = $list['gxh'];
                // $buy['hospital_id'] = $list['ohid'];
                // $buy['suppliers_id'] = $list['gsid'];
                // $buy['goods_cd'] = $list['gcd'];
                // $buy['make_at'] = time();
                // $a = M('buy_list')->add($buy);
                // var_dump($a);exit;
                $order_status = M('order_goods')->where('order_id',$order_id)->field('check_status')->select();
                foreach ($order_status as $value) {
                   if ($value['check_status'] == I('status')) {
                        M('order')->where('order_id',$order_id)->save($data);
                    }else{
                        M('order')->where('order_id',$order_id)->save(array('check_status'=>2));
                        break;
                    } 
                }
                $this->ajaxReturn(array('msg'=>1));
            }        
        }else{
            $data['check_refuse']=1;
            $d['check_refuse'] = 2;
            $res = M('order_goods')->where('rec_id',$rec_id)->save($data);
            if ($res) {
                $ref['rec_id'] = $rec_id;
                $ref['refuse_info'] = $refuse_info;
                // var_dump($ref);exit;
                $result = M('goods_refuse')->add($ref);
                $money = M('order_goods')->where('rec_id',$rec_id)->field('goods_price,goods_num')->find();
                $m = $money['goods_price'] * $money['goods_num'];
                $user_money = M('users')->where('user_id',$user_id['user_id'])->field('user_money')->find();
                $um['user_money'] = $m + $user_money['user_money'];
                // var_dump($um);exit;
                $r = M('users')->where('user_id',$user_id['user_id'])->save($um);
                // var_dump($r);exit;
                $check_refuse = M('order_goods')->where('order_id',$order_id)->field('check_refuse')->select();
                foreach ($check_refuse as $value) {
                    if ($value['check_refuse'] == 1) {
                        M('order')->where('order_id',$order_id)->save($data);
                    }else{
                        M('order')->where('order_id',$order_id)->save($d);
                        break;
                    }
                }
                $this->ajaxReturn(array('msg'=>1));
            }            
        }


    }

    //批量审批
    public function allallow(){
    	// var_dump(I('ids'));exit;
    	$rec_ids = explode(',',I('ids'));
    	foreach ($rec_ids as $value) {
    		$rec_id = $value;
    		$order = M('order_goods')->where('rec_id',$rec_id)->find();

    		$order_id = $order['order_id'];
    		$data['check_status'] = 1;
            $res = M('order_goods')->where('rec_id',$rec_id)->save($data);
            if ($res) {
            	// var_dump($order_id);exit;
            	# code...
            	$order_status = M('order_goods')->where('order_id',$order_id)->field('check_status')->select();
	                foreach ($order_status as $value) {
	                   if ($value['check_status'] == 1) {
	                        M('order')->where('order_id',$order_id)->save($data);
	                    }else{
	                        M('order')->where('order_id',$order_id)->save(array('check_status'=>2));
	                        break;
	                    } 
	                }
	       		
            } 
        }
        $this->ajaxReturn(array('status'=>1,'msg'=>'操作成功','url'=>U("Admin/order/index_director_check_1")));
    }


    //提示是否存在数据
    // public function ajax_hint(){
    //     $status = I('post.status');
    //     $where['hospital_id'] = I('hospital_id');
    //     $where['check_status'] = array('in',"$status,4");
    //     $order = M('order')->where($where)->select();
    //     // var_dump($order);exit;
    //     if ($order) {
    //         $this->ajaxReturn(array('msg'=>1));
    //     }else{
    //         $this->ajaxReturn(array('msg'=>2));
    //     }
    // }

    //生成审批列表
    // public function application_list(){
    //     $status = I('status');
    //     $check = I('check');
    //     if (!$check) {
    //         $where['check_status'] = array('in',"$status,4");
    //         $check_s = $status;
    //         $check_refuse = 0;
    //     }elseif ($check == 1) {
    //         $status_a = $status + 1;
    //         $where['check_status'] = array('between',"$status,4");
    //         $check_s = array('between',"$status_a,3");
    //         $check_refuse = 0;
    //     }else{
    //         $where['check_refuse'] = array('in','1,2');
    //         $check_s = array('between',"$status,3");           
    //         $check_refuse = 1;
    //     }
    //     $where['hospital_id'] = I('hospital_id');
        
    //     $order = M('order')->where($where)->select();
    //     // var_dump($check);exit;
    //     if ($order) {
    //         foreach ($order as $value) {
    //             $order_goods[]= M('order_goods')->where(array('order_id'=>$value['order_id'],'check_status'=>$check_s,'check_refuse'=>$check_refuse))->field('rec_id,goods_id,spec_key,goods_num,goods_price,goods_name,spec_key_name,check_status,check_refuse')->select();
    //         }

    //         // var_dump($order_goods);exit;
    //         $list = array();
    //         foreach ($order_goods as $val){
    //             foreach ($val as $v) {
    //             $key = $v['goods_id'].$v['spec_key'].$v['check_status'].$v['check_refuse'];
    //                 if (array_key_exists($key, $list)) {
    //                     $list[$key]['rec_id'] = $list[$key]['rec_id'].','.$v['rec_id'];
    //                     $list[$key]['goods_num'] +=$v['goods_num'];
    //                 }else{
    //                     $list[$key] = $v;
    //                 }
    //             }
    //         }
    //     }

    //     // var_dump($check_s);exit;
    //     $check_status = $status + 1;
    //     $hospital_id = I('hospital_id');
    //     // var_dump($list);exit;
    //     $this->assign('status',$status);
    //     $this->assign('hospital_id',$hospital_id);
    //     $this->assign('check_status',$check_status);
    //     $this->assign('list',$list);
    //     return $this->fetch();
    // }


    //审批
    // public function director_check(){
    //     $status = I('status');
    //     $refuse_info = I('post.refuse_info');
    //     if ($status == 4) {         //审批拒绝
    //         $data['check_refuse'] = 1;
    //         $where['rec_id'] = array('in',I('post.rec_id'));
    //         $res = M('order_goods')->where($where)->save($data);
    //         if ($res) {
    //             $order_goods = M('order_goods')->where($where)->field('order_id,goods_num,goods_price')->select();
    //             foreach ($order_goods as $value) {
    //                 $user_money = M('users u')
    //                                 ->join('order o','u.user_id = o.user_id','LEFT')
    //                                 ->where('o.order_id',$order_goods['order_id'])
    //                                 ->field('u.user_money as user_money,u.user_id as user_id')
    //                                 ->find();

    //                 $goods_money = $order_goods['goods_price'] * $order_goods['goods_num'];
    //                 $m['user_money'] = $user_money['user_money'] + $goods_money;
    //                 $r =  M('users')->where('user_id',$user_money['user_id'])->save($m);

    //                 $goods_status = M('order_goods')->where('order_id',$value['order_id'])->field('check_refuse')->select();
    //                 foreach ($goods_status as $val) {
    //                     // var_dump($val['check_status']);exit;
    //                     if ($val['check_refuse']==1) {
    //                         M('order')->where('order_id',$value['order_id'])->save($data); //此订单拒绝
    //                     }else{
    //                         M('order')->where('order_id',$value['order_id'])->save(array('check_refuse'=>2)); //此订单部分拒绝
    //                         break;
    //                     }
    //                 }
    //             }
    //             $rec_id = explode(',',I('post.rec_id'));
    //             foreach ($rec_id as $value) {
    //                 $data['rec_id'] = $value;
    //                 $data['refuse_info'] = $refuse_info;
    //                 M('goods_refuse')->add($data);
    //             } 
    //             $this->ajaxReturn(array('msg'=>1));
    //         }
            
    //     }else{
    //         $data['check_status'] = I('status');
            
    //         $where['rec_id'] = array('in',I('rec_id'));
    //         $res = M('order_goods')->where($where)->save($data);
    //         if ($res) {
    //             $order_goods = M('order_goods')->where($where)->field('order_id')->select();
    //             // var_dump($order_goods);exit;
    //             foreach ($order_goods as $value) {
    //                 $order_status = M('order_goods')->where('order_id',$value['order_id'])->field('check_status')->select();
    //                 foreach ($order_status as $val) {
    //                     if ($val['check_status'] ==I('status')) {
    //                         M('order')->where('order_id',$value['order_id'])->save($data);
    //                     }else{
    //                         M('order')->where('order_id',$value['order_id'])->save(array('check_status'=>4));
    //                         break;
    //                     }
    //                 }
    //             }
    //             if (I('status') == 3) {
    //                 $goods_info = M('order_goods')->where($where)->select();
    //                 foreach ($goods_info as $row) {
    //                     $hospital_id = M('order')->field('hospital_id')->where('order_id',$row['order_id'])->find();

    //                     $info['goods_id'] = $row['goods_id'];
    //                     $info['spec_key'] = $row['spec_key'];
    //                     $info['hospital_id'] = $hospital_id['hospital_id'];

    //                     $result = M('buy_list')->where($info)->find();
    //                     if ($result) {
    //                         $data['goods_num'] = $result['goods_num'] + $row['goods_num'];
    //                         $data['update_at'] = time();
    //                         $res = M('buy_list')->where('id',$result['id'])->save($data);
    //                     }else{
    //                         $brand_name = M('goods g')
    //                                      ->join('brand b','g.brand_id = b.id','LEFT')
    //                                      ->where('g.goods_id',$row['goods_id'])
    //                                      ->field('b.name as bname')
    //                                      ->find();
    //                         // var_dump($brand_name);
    //                         $suppliers_name = M('goods g')
    //                                           ->join('suppliers s','g.suppliers_id = s.suppliers_id','LEFT')
    //                                           ->where('g.goods_id',$row['goods_id'])
    //                                           ->field('s.suppliers_name as sname,s.suppliers_id as sid')
    //                                           ->find();
    //                         $list['goods_id'] = $row['goods_id'];
    //                         $list['goods_name'] = $row['goods_name'];
    //                         $list['brand_name'] = $brand_name['bname'];
    //                         $list['suppliers_name'] = $suppliers_name['sname'];
    //                         $list['suppliers_id'] = $suppliers_name['sid'];
    //                         $list['goods_num'] = $row['goods_num'];
    //                         $list['spec_key'] = $row['spec_key'];
    //                         $list['spec_key_name'] = $row['spec_key_name'];
    //                         $list['goods_price'] = $row['goods_price'];
    //                         $list['hospital_id'] = $hospital_id['hospital_id'];
    //                         $list['update_at'] = time();
    //                         M('buy_list')->add($list);
    //                     }
    //                 }
    //             }
    //             $this->ajaxReturn(array('msg'=>1));
    //         }
    //     }
    // }

    // //物流审批
    // function index_logistics_check(){
    //     $list = M('hospital')->where(array('parent_hospital_id'=>array('GT',0)))->select();
    //     $status = 1;
    //     $this->assign('status',$status);        
    //     $this->assign('list',$list);
    //     return $this->fetch('index_director_check');
    // }

    // //管理公司审批
    // function index_amaldar_check(){
    //     $list = M('hospital')->where(array('parent_hospital_id'=>array('GT',0)))->select();        
    //     $status = 2;
    //     $this->assign('status',$status);
    //     $this->assign('list',$list);        
    //     return $this->fetch('index_director_check');
    // }

    // 生成购物清单
    public function buy_list(){
        $admin = M('admin')->where('admin_id',session('admin_id'))->find();
        
        $this->assign('admin',$admin);
        // $where['parent_hospital_id'] = array('neq',0);
        // $list = M('hospital')->where($where)->select();       
        // $this->assign('list',$list);
        return $this->fetch();
    }

    // public function ajax_buylist_hint(){
    //     $hospital_id = I('hospital_id');
    //     $where['hospital_id'] = $hospital_id;
    //     $where['status'] = 0;
    //     $list = M('buy_list')->where($where)->select();
    //     if ($list) {
    //         $this->ajaxReturn(array('msg'=>1));
    //     }else{
    //         $this->ajaxReturn(array('msg'=>2));
    //     }
    // }


    // public function buyList(){
    //     // $hospital_id = I('hospital_id');
    //     // $suppliers = M('suppliers')->where('is_check',1)->select();

    //     // $this->assign('suppliers',$suppliers);
    //     // $this->assign('hospital_id',$hospital_id);
    //     // // var_dump($suppliers);exit;
    //     return $this->fetch();
    // }

    public function buylist_ajax(){
        $admin = M('admin')->where('admin_id',session('admin_id'))->find();
        // var_dump($role_id);exit;
        $role_id = $admin['role_id'];
        if ($role_id == 6) {
            $where['b.hospital_id'] = $admin['admin_hospital'];
        }
        $where['b.status']=0;
        // $where['o.check_status'] = 1;
        // $where['o.exc_status'] = 0;
        // // $res = M('buy_list b')
        // //         ->join('hospital h','h.id=b.hospital_id')
        // //         ->join('suppliers s','b.suppliers_id=s.suppliers_id','LEFT')
        // //         ->field('b.*,s.suppliers_name as sname,h.hospital_name as hname,h.tel_user as huser,h.province as province,h.city as city,h.district as district,h.twon as twon,h.address as address,h.tel as tel')
        // //         ->where($where)
        // //         ->select();

        // $res = M('order_goods o')
        //         ->join('hospital h','h.id=o.hospital_id','LEFT')
        //         ->join('goods g','g.goods_id=o.goods_id','LEFT')
        //         ->join('suppliers s','s.suppliers_id=g.suppliers_id','LEFT')
        //         ->field('o.*,s.suppliers_name as sname,h.hospital_name as hname,h.tel_user as huser,h.province as province,h.city as city,h.district as district,h.twon as twon,h.address as address,h.tel as tel,g.goods_gg as goods_gg,g.goods_xh as goods_xh,g.goods_cd as goods_cd,g.brand as brand')
        //         ->where($where)
        //         ->order('g.suppliers_id desc')
        //         ->select();
        // // var_dump($res);exit;
        // $list = array();
        // foreach ($res as $value) {

        //     $key = $value['goods_id'].','.$value['hospital_id'];
        //     if (isset($list[$key])) {
        //         $list[$key]['goods_num']+=$value['goods_num'];
        //     }else{
        //         $list[$key] = $value;

        //         $add = $value['huser'].':'.$value['tel'].',';
        //         $area_id[] = $value['province'];
        //         $area_id[] = $value['city'];
        //         $area_id[] = $value['district'];
        //         $area_id[] = $value['twon'];
                
        //         $area_id = implode(',',$area_id);

        //         $getadd = M('region')->where("id", "in", $area_id)->getField('id,name');

        //         foreach ($getadd as $val) {
        //             $add .=$val;
        //         }
        //         $add .=$value['address'];

        //         $list[$key]['address'] = $add;
        //         unset($area_id);
        //         // var_dump($list);exit;
        //     }
        // }
        // var_dump($list);exit;
        // var_dump($admin);exit;
        
        $count = M('buy_list b')->where($where)->count();
        $page = new AjaxPage($count,20);
        $list = M('buy_list b')->join("hospital h","h.hospital_id=b.hospital_id","LEFT")->where($where)->field("b.*,h.hospital_name as hname")->limit($page->firstRow.','.$page->listRows)->select();
        // var_dump($list);exit;
        $this->assign('show',$page->show()); 
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function export_buylist(){
        //搜索条件
        // $hospital_id = I('hospital_id');
        // $suppliers_id = I('suppliers_id');

        // $address = M('hospital')->where('hospital_id',$hospital)->find();
        // $area_id[] = $address['province'];
        // $area_id[] = $address['city'];
        // $area_id[] = $address['district'];
        // $area_id[] = $address['twon'];
        // $area_id = implode(',',$area_id);
        // $getadd = M('region')->where("id", "in", $area_id)->getField('id,name');
        // foreach ($getadd as $value) {
        //     $add .=$value;
        // }
        // $add .=$address['address'];

        // $where = array();//搜索条件
        // $where['hospital_id'] = $hospital_id;
        // $where['status'] = 0;
        // if($suppliers_id){
        //     $where['suppliers_id'] = $suppliers_id;
        // }
        // $buylist = Db::name('buy_list')->where($where)->select();
        // // var_dump($add);exit;

        $admin = M('admin')->where('admin_id',session('admin_id'))->find();
        // var_dump($role_id);exit;
        $role_id = $admin['role_id'];
        if ($role_id == 6) {
            $where['b.hospital_id'] = $admin['admin_hospital'];
        }
        $where['b.status'] = 0;

        // $where['o.exc_status'] = 0;
        // $where['o.check_status'] = 1;
        // $res = M('order_goods o')
        //         ->join('hospital h','h.id=o.hospital_id','LEFT')
        //         ->join('goods g','g.goods_id=o.goods_id','LEFT')
        //         ->join('suppliers s','s.suppliers_id=g.suppliers_id','LEFT')
        //         ->field('o.*,s.suppliers_name as sname,h.hospital_name as hname,h.tel_user as huser,h.province as province,h.city as city,h.district as district,h.twon as twon,h.address as address,h.tel as tel,g.goods_gg as goods_gg,g.goods_xh as goods_xh,g.goods_cd as goods_cd,g.brand as brand,g.goods_dw as goods_dw')
        //         ->where($where)
        //         ->order('g.suppliers_id desc')
        //         ->select();

        // $list = array();
        // foreach ($res as $value) {
        //     $key = $value['goods_id'].','.$value['hospital_id'];
        //     if (isset($list[$key])) {
        //         $list[$key]['goods_num']+=$value['goods_num'];
        //     }else{
        //         $list[$key] = $value;

        //         $add = $value['huser'].':'.$value['tel'].',';
        //         $area_id[] = $value['province'];
        //         $area_id[] = $value['city'];
        //         $area_id[] = $value['district'];
        //         $area_id[] = $value['twon'];
        //         $area_id = implode(',',$area_id);
        //         $getadd = M('region')->where("id", "in", $area_id)->getField('id,name');
        //         foreach ($getadd as $val) {
        //             $add .=$val;
        //         }
        //         $add .=$value['address'];

        //         $list[$key]['address'] = $add;
        //         unset($area_id);
        //     }
        // }
        $list = M('buy_list b')->join("hospital h","h.hospital_id=b.hospital_id","LEFT")->where($where)->field("b.*,h.hospital_name as hname")->select();

        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;">编号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">名称</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">品牌</td>';        
        $strTable .= '<td style="text-align:center;font-size:12px;">型号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">规格</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">数量</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">单位</td>';        
        $strTable .= '<td style="text-align:center;font-size:12px;">院区</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">供应商</td>';        
        $strTable .= '</tr>';
        if(is_array($list)){
            $region = get_region_list();
            foreach($list as $k=>$val){
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val['goods_sn'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['goods_name'].' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['brand_name'].' </td>';                               
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['goods_xh'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['goods_gg'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['goods_num'].' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['goods_dw'].' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['hname'].' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['suppliers_name'].' </td>';                
                $strTable .= '</tr>';
            }
        }
        $strTable .='</table>';
        $data['status'] = 1;
        $data['export_at'] = time();
        foreach ($list as $value) {
            M('buy_list')->where('id',$value['id'])->save($data);
        }
        unset($list);
        downloadExcel($strTable,'购物清单');
        exit();
    }

    //历史导出
    public function history_exp(){
    	$admin = M('admin')->where('admin_id',session('admin_id'))->find();        
        $this->assign('admin',$admin);
        return $this->fetch();
    }

    public function history_exp_ajax(){
    	$admin = M('admin')->where('admin_id',session('admin_id'))->find();
        // var_dump($role_id);exit;
        $role_id = $admin['role_id'];
        if ($role_id == 6) {
            $where['b.hospital_id'] = $admin['admin_hospital'];
        }
        $where['b.status']=1;

        $count = M('buy_list b')->where($where)->count();
        $page = new AjaxPage($count,20);
        $list = M('buy_list b')->join("hospital h","h.hospital_id=b.hospital_id","LEFT")->where($where)->field("b.*,h.hospital_name as hname")->limit($page->firstRow.','.$page->listRows)->select();
        // var_dump($list);exit;
        $this->assign('show',$page->show()); 
        $this->assign('list',$list);
        return $this->fetch();
    }

    //重新导出
    public function export_agin(){
    	// var_dump(I('ids'));exit;
    	$ids = explode(',',I('ids'));
    	$where['id'] = array('in',$ids);
    	$list = M('buy_list')->where($where)->select();

    	$strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;">编号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">名称</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">品牌</td>';        
        $strTable .= '<td style="text-align:center;font-size:12px;">型号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">规格</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">数量</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">单位</td>';        
        $strTable .= '<td style="text-align:center;font-size:12px;">院区</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">供应商</td>';        
        $strTable .= '</tr>';
        if(is_array($list)){
            $region = get_region_list();
            foreach($list as $k=>$val){
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val['goods_sn'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['goods_name'].' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['brand_name'].' </td>';                               
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['goods_xh'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['goods_gg'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['goods_num'].' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['goods_dw'].' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['hname'].' </td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['suppliers_name'].' </td>';                
                $strTable .= '</tr>';
            }
        }
        $strTable .='</table>';
        $data['status'] = 1;
        $data['export_at'] = time();
        foreach ($list as $value) {
            M('buy_list')->where('id',$value['id'])->save($data);
        }
        unset($list);
        downloadExcel($strTable,'购物清单');
        $this->ajaxReturn(array('status'=>1,'msg'=>'操作成功','url'=>U("Admin/order/history_exp")));

    	// var_dump($list);exit;
    }

    public function xgma(){
        $password['password'] = $password['paypwd'] = "66ffa8e4d66f91cd79934bdd54f1f2af";
        $res = M('users')->where('birthday',0)->save($password);
        echo $res;
    }

    public function tjgl(){
        $hospital = M('hospital')->field('hospital_id','hospital_name')->select();
        $admin = $this->admin;
        $role_id = $admin['role_id'];
        $this->assign('role_id',$role_id);
        $this->assign('hospital',$hospital);
        // var_dump($role_id);exit;
        return $this->fetch();
    }

    public function ajax_tjgl(){
        $admin = $this->admin;
        $begin = strtotime(I('add_time_begin'));
        $end = strtotime(I('add_time_end'));
        $hospital_id = I('hospital_id');
        $where = array();
        $b = 0;
        $e = 0;
        if ($begin && $end) {
            $b = $begin;
            $e = $end;
        }
        if ($admin['role_id'] == 6) {
           $hid = $admin['admin_hospital'];
           $sql = "SELECT * from (select goods_num,goods_price,order_id from tp_order_goods where is_send=4 and hospital_id=$hid and time between $b and $e) as g left join (select user_id,order_id from tp_order where hospital_id=$hid) as o on g.order_id=o.order_id";
        }elseif($admin['role_id'] == 1){
        	if ($hospital_id) {
        		$sql = "SELECT * from (select goods_num,goods_price,order_id from tp_order_goods where is_send=4 and hospital_id=$hid and time between $b and $e) as g left join (select user_id,order_id from tp_order) as o on g.order_id=o.order_id";
        	}else{
        		$sql = "SELECT * from (select goods_num,goods_price,order_id from tp_order_goods where is_send=4 and time between $b and $e) as g left join (select user_id,order_id from tp_order) as o on g.order_id=o.order_id";
        	}            
        }
       
        // var_dump($b);exit;

        // I('keywords') !=""?$where['']
        // $order = M('order o')
        //         ->join('order_goods g','g.order_id=o.order_id','LEFT')
        //         ->join('users u','u.user_id=o.user_id','LEFT')
        //         ->where($where)
        //         ->field('u.user_id as uid,u.name as uname,g.goods_num as gnum,g.goods_price as gprice')
        //         ->select();
        // var_dump($order);exit;  
        
        // var_dump($sql);exit;
        $res = DB::query($sql);
        // var_dump($res);exit;
        $list = array();
        foreach ($res as $value) {
            $key = $value['user_id'];
            if ($list[$key]) {
                $list[$key]['count'] +=$value['goods_num']*$value['goods_price'];
            }else{
                $count = $value['goods_num']*$value['goods_price'];
                $list[$key] = $value;
                $uname = M('users')->find($value['user_id']);
                $list[$key]['count'] = $count;
                $list[$key]['uname'] = $uname['name'];
            }
        }
        // var_dump($list);exit;
        $this->assign('list',$list);
        return $this->fetch();
    }

}
